<template>
  <el-card>
    <h4 class="mb-20px">查看详情</h4>
    <!-- <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="平台信息id" prop="platformInformationId">
        <el-input v-model="formData.platformInformationId" placeholder="请输入平台信息id" />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户id" />
      </el-form-item>
    </el-form> -->
    <el-row>
      <el-col :span="2"> 报名ID： </el-col>
      <el-col :span="8">{{ formData.id }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 用户ID： </el-col>
      <el-col :span="8">{{ formData.userId }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 用户昵称： </el-col>
      <el-col :span="8">{{ formData.nickname }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 用户手机号： </el-col>
      <el-col :span="8">{{ formData.mobile }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 所属分类： </el-col>
      <el-col :span="8">{{ formData.category || '-' }}</el-col>
    </el-row>
    <el-row class="mt-20px" v-for="(value, key) in formData.registrationNames" :key="key">
      <el-col :span="2"> {{ key }}： </el-col>
      <el-col :span="8">{{ value }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 所属栏目： </el-col>
      <el-col :span="8">{{ formData.titleType }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 信息ID： </el-col>
      <el-col :span="8">{{ formData.platformInformationId }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 标题： </el-col>
      <el-col :span="8">{{ formData.title }}</el-col>
    </el-row>
    <el-row class="mt-20px">
      <el-col :span="2"> 创建时间： </el-col>
      <el-col :span="8">{{ formatDate(formData.createTime) }}</el-col>
    </el-row>
    <div class="mt-20px">
      <!-- <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> -->
      <el-button
        @click="
          () => {
            $router.back()
            delVisitedView(route)
          }
        "
        >返回</el-button
      >
    </div>
  </el-card>
</template>
<script setup lang="ts">
import {
  PlatformRegistrationInformationApi,
  PlatformRegistrationInformation
} from '@/api/biz/platformregistrationinformation'
import { formatDate } from '@/utils/formatTime'
import { useTagsViewStore } from '@/store/modules/tagsView'

const { delVisitedView } = useTagsViewStore()
const route = useRoute()

/** 平台报名信息 表单 */
defineOptions({ name: 'PlatformRegistrationInformationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const id = +useRoute().params.id

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  platformInformationId: undefined,
  userId: undefined,
  nickname: undefined,
  mobile: undefined,
  category: undefined,
  titleType: undefined,
  title: undefined,
  createTime: new Date(),
  phone: undefined,
  company: undefined,
  attendeeName: undefined,
  position: undefined,
  registrationNames: {}
})
const formRules = reactive({
  platformInformationId: [{ required: true, message: '平台信息id不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '用户id不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data = await PlatformRegistrationInformationApi.getPlatformRegistrationInformation(id)
      if (data.registrationNames) {
        data.registrationNames = JSON.parse(data.registrationNames)
      } else {
        data.registrationNames = {}
      }
      formData.value = data
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PlatformRegistrationInformation
    if (formType.value === 'create') {
      await PlatformRegistrationInformationApi.createPlatformRegistrationInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await PlatformRegistrationInformationApi.updatePlatformRegistrationInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    platformInformationId: undefined,
    userId: undefined,
    nickname: undefined,
    mobile: undefined,
    category: undefined,
    titleType: undefined,
    title: undefined,
    createTime: new Date(),
    phone: undefined,
    company: undefined,
    attendeeName: undefined,
    position: undefined,
    registrationNames: {}
  }
  formRef.value?.resetFields()
}

open('update', id)
</script>
